package com.cpkso.buss.backend.user.service;

import java.util.Date;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cpkso.buss.backend.exception.EntityHasExistException;
import com.cpkso.buss.backend.exception.GivenNullParamException;
import com.cpkso.buss.backend.exception.NoSuchDataException;
import com.cpkso.buss.backend.user.dao.PermissionDao;
import com.cpkso.buss.backend.user.domain.Permission;
import com.runze.base.web.Response;

/**
 * 权限分配service
 * 
 * 按name查找权限
 * 新增权限
 * 修改权限
 * 删除权限
 * 
 * 显示所有权限
 * 按id查找权限
 * 
 * @author wrzhxy@qq.com
 * @date 2018年3月18日
 */
@Service
@Transactional
public class PermissionService {
	/**
	 * 按姓名查找权限
	 * @param name
	 * @return
	 */
	public Permission findByName(String name) {
		return permissionDao.findByName(name);
	}
	
	/**
	 * 创建权限
	 * @param permission
	 */
	public void create(Permission permission) throws GivenNullParamException, EntityHasExistException {
		if (permission == null || !StringUtils.hasText(permission.getName())) throw new GivenNullParamException();
		String name = permission.getName();
		if (findByName(name) != null) throw new EntityHasExistException();
		permission.setCreateTime(new Date());
		permission.setUpdateTime(null);
		permissionDao.save(permission);
	}
	/**
	 * 批量创建权限
	 * @param permissions
	 */
//	public void create(List<Permission> permissions) {
//		for (Permission permission : permissions) {
//			if (permission == null || !StringUtils.hasText(permission.getName())) throw new GivenNullParamException();
//			String name = permission.getName();
//			if (findByName(name) != null) throw new EntityHasExistException();
//			permission.setCreateTime(new Date());
//			permission.setUpdateTime(null);
//		}
//		permissionDao.saveAll(permissions);
//	}
	/**
	 * 修改权限
	 */
	public void update(Permission permission) throws GivenNullParamException, NoSuchDataException {
		if (permission == null || permission.getId() == null) throw new GivenNullParamException();
		Optional<Permission> permissionDbOpt = permissionDao.findById(permission.getId());
		Permission permissionDb = permissionDbOpt.get();
		if (permissionDb == null) throw new NoSuchDataException();
		String name = permission.getName();
		if (StringUtils.hasText(name)) permissionDb.setName(name);
		String description = permission.getDescription();
		if (StringUtils.hasText(description)) permissionDb.setDescription(description);
		permissionDb.setUpdateTime(new Date());
		permissionDao.save(permissionDb);
	}
	
	/**
	 * 删除权限
	 * @param id
	 */
	public void del(long id) {
		permissionDao.deleteById(id);
	}
	
	/**
	 * 显示所有权限
	 * @return
	 */
	public Response findAll() {
		return new Response(permissionDao.findAll());
	}
	
	/**
	 * 按角色寻找权限
	 * @param id
	 * @return
	 */
	public Response findByRoleId(Long id) {
		return new Response(permissionDao.findByRolesId(id));
	}
	
	/**
	 * 按id查找权限
	 * @param id
	 * @return
	 */
	public Permission findById(Long id) {
		return permissionDao.findById(id).get();
	}
	
	@Autowired
	private PermissionDao permissionDao;

}
